﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Common;
using Basic.VO;
using Basic.Business;

public partial class Basic_Background_AdminLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Buffer = true;
        Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
        Response.Expires = 0;
        Response.CacheControl = "no-cache";
        Response.AppendHeader("Pragma", "No-Cache");

        if (!String.IsNullOrEmpty(Request.QueryString["Action"]))
        { 
            string action=BadChar.ReplaceBadChar(Request.QueryString["Action"]);
            switch (action)
            {
                case "login":
                    string safeCode = BadChar.ReplaceBadChar(Request.Form["safeCode"]).ToUpper();
                    if (Session["SafeCode"]==null)
                        Tools.ShowInfo(StringTable.ErrorSafeCode, "../Background/AdminLogin.aspx", "_self");
                    if (!safeCode.Equals((string)Session["SafeCode"]))
                    {
                        Session["SafeCode"] = null;
                        Tools.ShowInfo(StringTable.ErrorSafeCode, "../Background/AdminLogin.aspx", "_self");
                    }
                    Admin admin = AdminAdmin.Get(BadChar.ReplaceBadChar(Request.Form["adminName"]), Tools.MD5Encrypt(Request.Form["adminPass"]));
                    if (admin == null)
                        Tools.ShowInfo(StringTable.ErrorPassWord, "../Background/AdminLogin.aspx", "_self");
                    else
                    {
                        admin.loginTimes+=1;
                        Session["admin"] = admin;
                        Session.Timeout = 25;
                        AdminAdmin.Update(admin.adminId, Tools.GetIpAddress(), DateTime.Now, admin.loginTimes);
                        Response.Redirect("AdminMain.aspx");
                    }
                    break;
                default:
                    if (Session["admin"] != null)
                        Session["admin"] = null;
                    break;
            }
        }
    }
}
